{% extends "admin/layout.html" %}

{% block title %}操作日志 - 后台管理系统{% endblock %}

{% block page_title %}操作日志{% endblock %}

{% block breadcrumb %}
<li class="breadcrumb-item active">操作日志</li>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="card shadow-sm">
        <div class="card-header bg-white py-3">
            <h5 class="mb-0">日志筛选</h5>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <!-- 搜索框 -->
                <div class="col-12 col-md-6 col-lg-3">
                    <label class="form-label small fw-semibold">关键词搜索</label>
                    <div class="input-group">
                        <input type="text" name="search" class="form-control form-control-sm" placeholder="搜索日志内容..."
                               value="{{ request.args.get('search', '') }}">
                        <button type="submit" class="btn btn-sm btn-outline-secondary">
                            <i class="fas fa-search"></i>
                        </button>
                    </div>
                </div>

                <!-- 操作员筛选 -->
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label small fw-semibold">操作员</label>
                    <select name="admin_id" class="form-select form-select-sm">
                        <option value="">所有操作员</option>
                        {% for admin in admins %}
                        <option value="{{ admin.id }}" {% if request.args.get('admin_id')|int == admin.id %}selected{% endif %}>
                            {{ admin.username }}
                        </option>
                        {% endfor %}
                    </select>
                </div>

                <!-- 操作类型筛选 -->
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label small fw-semibold">操作类型</label>
                    <select name="action" class="form-select form-select-sm">
                        <option value="">所有操作</option>
                        <option value="添加" {% if request.args.get('action') == '添加' %}selected{% endif %}>添加</option>
                        <option value="编辑" {% if request.args.get('action') == '编辑' %}selected{% endif %}>编辑</option>
                        <option value="删除" {% if request.args.get('action') == '删除' %}selected{% endif %}>删除</option>
                        <option value="登录" {% if request.args.get('action') == '登录' %}selected{% endif %}>登录</option>
                    </select>
                </div>

                <!-- 资源类型筛选 -->
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label small fw-semibold">资源类型</label>
                    <select name="resource" class="form-select form-select-sm">
                        <option value="">所有资源</option>
                        <option value="AdminUser" {% if request.args.get('resource') == 'AdminUser' %}selected{% endif %}>用户</option>
                        <option value="AdminPermission" {% if request.args.get('resource') == 'AdminPermission' %}selected{% endif %}>权限</option>
                        <option value="AdminDepartment" {% if request.args.get('resource') == 'AdminDepartment' %}selected{% endif %}>部门</option>
                    </select>
                </div>

                <!-- IP地址筛选 -->
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label small fw-semibold">IP地址</label>
                    <input type="text" name="ip_address" class="form-control form-control-sm" placeholder="输入IP地址"
                           value="{{ request.args.get('ip_address', '') }}">
                </div>

                <!-- 日期范围筛选 -->
                <div class="col-12 col-md-6 col-lg-3">
                    <label class="form-label small fw-semibold">操作时间范围</label>
                    <div class="input-group input-group-sm">
                        <input type="date" name="start_date" class="form-control"
                               value="{{ request.args.get('start_date', '') }}">
                        <span class="input-group-text">至</span>
                        <input type="date" name="end_date" class="form-control"
                               value="{{ request.args.get('end_date', '') }}">
                    </div>
                </div>

                <!-- 操作按钮 -->
                <div class="col-12 col-md-6 col-lg-2">
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-sm btn-primary flex-grow-1">
                            <i class="fas fa-filter me-1"></i> 应用筛选
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-secondary" onclick="location.href='{{ url_for('admin.admin_log.operation_logs') }}'">
                            <i class="fas fa-sync-alt"></i>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card shadow-sm mt-4">
        <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
            <h5 class="mb-0">操作日志列表</h5>
            <div class="text-muted small">共 {{ logs.total }} 条记录</div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-striped mb-0">
                    <thead class="table-light">
                        <tr>
                            <th width="160px" class="ps-4">操作时间</th>
                            <th width="120">操作员</th>
                            <th width="100">操作类型</th>
                            <th width="120">操作对象</th>
                            <th>操作详情</th>
                            <th width="120">IP地址</th>
                            <th width="80">状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in logs.items %}
                        <tr>
                            <td class="ps-4">{{ log.formatted_time }}</td>
                            <td><span class="badge bg-light text-dark">{{ log.admin.username if log.admin else '系统' }}</span></td>
                            <td>
                                {% if log.action == '添加' %}
                                    <span class="badge bg-success bg-opacity-10 text-success">{{ log.action }}</span>
                                {% elif log.action == '编辑' %}
                                    <span class="badge bg-primary bg-opacity-10 text-primary">{{ log.action }}</span>
                                {% elif log.action == '删除' %}
                                    <span class="badge bg-danger bg-opacity-10 text-danger">{{ log.action }}</span>
                                {% else %}
                                    <span class="badge bg-secondary bg-opacity-10 text-secondary">{{ log.action }}</span>
                                {% endif %}
                            </td>
                            <td><span class="badge bg-light text-dark">{{ log.resource }}</span></td>
                            <td class="text-truncate" style="max-width: 300px;" title="{{ log.details }}">{{ log.details }}</td>
                            <td><code>{{ log.ip_address }}</code></td>
                            <td>
                                {% if log.success %}
                                <span class="badge bg-success"><i class="fas fa-check-circle me-1"></i>成功</span>
                                {% else %}
                                <span class="badge bg-danger"><i class="fas fa-times-circle me-1"></i>失败</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="7" class="text-center py-4 text-muted">
                                <i class="fas fa-inbox fa-2x mb-2"></i>
                                <p>暂无日志记录</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if logs.pages > 1 %}
            <div class="card-footer bg-white">
                <nav aria-label="Page navigation">
                    <ul class="pagination pagination-sm justify-content-center mb-0">
                        {% if logs.has_prev %}
                        <li class="page-item">
                            <a class="page-link" href="{{ url_for('admin.admin_log.operation_logs', **dict(request.args, page=logs.prev_num)) }}">
                                <i class="fas fa-chevron-left"></i>
                            </a>
                        </li>
                        {% endif %}

                        {% for page_num in logs.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
                            {% if page_num %}
                                <li class="page-item {% if page_num == logs.page %}active{% endif %}">
                                    <a class="page-link" href="{{ url_for('admin.admin_log.operation_logs', **dict(request.args, page=page_num)) }}">{{ page_num }}</a>
                                </li>
                            {% else %}
                                <li class="page-item disabled"><span class="page-link">...</span></li>
                            {% endif %}
                        {% endfor %}

                        {% if logs.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="{{ url_for('admin.admin_log.operation_logs', **dict(request.args, page=logs.next_num)) }}">
                                <i class="fas fa-chevron-right"></i>
                            </a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}